/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gfs.location;

/**
 *
 * @author Joel
 */
public class Location extends Coordinate implements Cloneable
    {
    /**
     * The scale of the grid that this location object will be used
     * ex. the scale of a 10x10 array would be 10
     */
    private int scale;
    
    /**
     * The row of this Location 
     */
    private int row;
    
    /** 
     * The col of this Location 
     */
    private int col;    
    
    /**
     * Constructs a <tt>Location</tt> with the specified row and column
     * 
     * @param row
     *      The row of this Location
     * @param col
     *      The column of this Location
     */
    public Location(int row, int col, int scale)
        {
        super();
        this.scale = scale;
        this.row = row;
        this.col = col;
        xC = 60 * col - 270;
        yC = 60 * (9 - row) - 270;
        }
    
    public Location(int scale)
        {
        this(0, 0, 8);
        }
    
    /**
     * Returns a Location that would have the given hash code value
     * 
     * @param hash
     *      The hash code of the location object you want
     * @return
     *      The location object that has the given hash code
     */
    public Location getLocFromHash(int hash) 
        {
        if(hash >= 0)
            if(hash < scale)
                return new Location(0, hash, scale);
            else
                {
                int col = (hash % 10), row = (hash / 10);
                return new Location(row, col, scale);
                }
        else
            return null;
        }

    public int getCol() 
        {
        return col;
        }

    public int getRow() 
        {
        return row;
        }

    public boolean modCol(int val)
        {        
        if(col + val < scale && col + val > -1)
            {
            col += val;
            return true;
            }
        else
            return false;
        }

    public boolean modRow(int val)
        {
        if(row + val < scale && row + val > -1)
            {
            row += val;
            return true;
            }
        else
            return false;
        }

    public boolean modLoc(int rowMod, int colMod)
        {
        int oRow = row, oCol = col;
        if(modRow(rowMod) && modCol(colMod))
            return true;
        else
            {
            row = oRow;
            col = oCol;
            return false;
            }
        }

    /**
     * Returns a clone of this <tt>Location</tt>
     *
     * @return
     *      A clone of this <tt>Location</tt>
     */
    @Override
    public Object clone()
        {
        return super.clone();
        }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Location other = (Location) obj;
        if (this.row != other.row) {
            return false;
        }
        if (this.col != other.col) {
            return false;
        }
        return true;
    }

    @Override   
    public int hashCode() 
        {
        return getRow() * 10 + getCol();
        }

    @Override
    public String toString() 
        {
        return getClass().getSimpleName() + " (" + row + "," + col + ")";
        }
    
    }
